package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author mrj
 * @date 2025-08-08
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanVoById(Long id)
    {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan , nursingPlanVo);
        //根据护理计划id查询护理计划项目关联表
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectNursingProjectPlanVoById(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setSortNo(nursingPlanDto.getSortNo());
        nursingPlan.setPlanName(nursingPlanDto.getPlanName());
        nursingPlan.setStatus(nursingPlanDto.getStatus());
        //当两个类的属性名相同且类型兼容的情况下，可以使用属性拷贝的方式快速构造实体
        BeanUtils.copyProperties(nursingPlanDto , nursingPlan);
        int row = nursingPlanMapper.insert(nursingPlan);
        if(row <= 0){
            throw new BaseException("新增护理计划失败");
        }
        //2.批量保存护理计划与护理项目的关联关系
        nursingPlanDto.getProjectPlans()
                .forEach(projectPlan -> {projectPlan.setPlanId(nursingPlan.getId());});
        row = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans());
        if(row <= 0){
            throw new BaseException("批量保存护理计划与护理项目的关联关系失败");
        }

        return 1 ;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto , nursingPlan);
        int row = nursingPlanMapper.updateById(nursingPlan);
        if(row <= 0){
            throw new BaseException("修改护理计划失败");
        }
        //2.批量保存护理计划与护理项目的关联关系
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(StringUtils.isNotEmpty(projectPlans)){
            //2.1先删除
            Long planId = nursingPlanDto.getId();
            row = nursingProjectPlanMapper.deleteByPlanId(planId);
            if(row <= 0){
                throw new BaseException("批量保存护理计划与护理项目的关联关系失败【删除失败】");
            }
            //2.2再保存
            projectPlans.forEach(projectPlan -> {projectPlan.setPlanId(planId);});
            row = nursingProjectPlanMapper.batchInsert(projectPlans);
            if(row <= 0){
                throw new BaseException("批量保存护理计划与护理项目的关联关系失败【新增失败】");
            }
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Long id)
    {
        int row = nursingPlanMapper.deleteById(id);
        if(row <= 0){
            throw new BaseException("删除护理计划失败");
        }
        try{
            nursingProjectPlanMapper.deleteByPlanId(id);
        }catch(Exception e){
            throw new BaseException("删除护理计划与项目关联关系失败");
        }
        return 1;
    }
}
